import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/widgets/circular.dart';

import '../../../utils/utils.dart';
import '../../../widgets/base/qkd_text.dart';

class TimeCountdownView extends StatefulWidget {
  final String time;
  final bool isShare;
  final bool isVideo;
  const TimeCountdownView(this.time,
      {super.key, this.isShare = false, this.isVideo = false});

  @override
  State<TimeCountdownView> createState() => _TimeCountdownViewState();
}

class _TimeCountdownViewState extends State<TimeCountdownView> {
  late int interval;
  int hour = 0;
  int min = 0;
  int sec = 0;
  Timer? timer;

  @override
  void initState() {
    super.initState();
    if (widget.time.isEmpty) {
      interval = 0;
    } else {
      final begin =
          DateTime.parse(widget.time).toLocal().millisecondsSinceEpoch;
      final current = DateTime.now().millisecondsSinceEpoch;
      interval = begin - current;
    }

    getTime();
    timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      interval = interval - 1000;
      if (interval < 0) {
        timer.cancel();
      } else {
        getTime();
        update();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    String text = '支付后可查看全部内容';
    if (widget.isShare) {
      text = '扫码查看专家方案';
    } else if (widget.isVideo) {
      text = '支付后可查看视频讲解';
    }
    return Circular(
      radius: 8,
      clipBehavior: Clip.antiAlias,
      child: Stack(
        children: [
          if (widget.isVideo)
            Positioned.fill(
                child: ColorFiltered(
              colorFilter: ColorFilter.mode(
                  Colours.black.withOpacity(0.5), BlendMode.color),
              child: Image.asset(
                Utils.getImgPath("plan_video_count.png"),
                fit: BoxFit.cover,
              ),
            )),
          Container(
              height: widget.isVideo ? 188 : 120,
              decoration: BoxDecoration(
                  color: Colours.main.withOpacity(0.1),
                  border: Border.all(
                      width: 0.5, color: Colours.main.withOpacity(0.5))),
              alignment: Alignment.center,
              child: DefaultTextStyle(
                  style: const TextStyle(color: Colours.white, fontSize: 12),
                  child: Column(mainAxisSize: MainAxisSize.min, children: [
                    if (widget.isShare)
                      Container(
                          width: 46,
                          height: 46,
                          margin: const EdgeInsets.only(bottom: 8),
                          alignment: Alignment.center,
                          decoration: const BoxDecoration(
                              color: Colours.white, shape: BoxShape.circle),
                          child: Image.asset(Utils.getImgPath("ai_lock.png"),
                              width: 24)),
                    QkdText(text,
                        color: widget.isVideo ? Colours.white : Colours.main,
                        fontSize: 14,
                        fontWeight: FontWeight.w500),
                    if (!widget.isShare) time().paddingOnly(top: 16)
                  ]))),
        ],
      ),
    );
  }

  Widget time() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        QkdText('距售卖截止',
            fontSize: 12,
            color: widget.isVideo ? Colours.white : Colours.grey66),
        const SizedBox(width: 8),
        timeCard(formatTime(hour)),
        QkdText(':',
                needCenter: true,
                color: widget.isVideo ? Colours.white : Colours.main,
                fontSize: 20,
                fontWeight: FontWeight.w600)
            .marginOnly(left: 3, right: 3),
        timeCard(formatTime(min)),
        QkdText(':',
                needCenter: true,
                color: widget.isVideo ? Colours.white : Colours.main,
                fontSize: 20,
                fontWeight: FontWeight.w600)
            .marginOnly(left: 3, right: 3),
        timeCard(formatTime(sec)),
      ],
    );
  }

  String formatTime(int time) {
    if (time < 10) {
      return '0$time';
    } else {
      return '$time';
    }
  }

  Widget timeCard(String text) {
    return Container(
        width: 28,
        height: 28,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: widget.isVideo ? Colours.black : Colours.white,
            borderRadius: BorderRadius.circular(4),
            border: Border.all(
                color: widget.isVideo
                    ? Colours.transparent
                    : Colours.main.withOpacity(0.5),
                strokeAlign: BorderSide.strokeAlignOutside)),
        child: QkdText(text,
            color: widget.isVideo ? Colours.white : Colours.main,
            fontSize: 16,
            fontWeight: FontWeight.w500,
            fontFamily: "Din"));
  }

  getTime() {
    if (interval < 0) {
      return;
    }
    int intervalTemp = interval;
    hour = intervalTemp ~/ (60 * 60 * 1000);
    intervalTemp -= hour * (60 * 60 * 1000);
    min = intervalTemp ~/ (60 * 1000);
    intervalTemp -= min * (60 * 1000);
    sec = intervalTemp ~/ 1000;
  }
}
